import logging

import pytest

from resource.pom.memorandum import Memorandum
from utils.manager import Images


class Test:
    app = Memorandum

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1.点击长按已存在的文件夹')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.click(*self.app.open_menu)
        device.refresh_layout()
        personal = device.get_element_by_text('个人')
        device.long_click_element(personal)
        logging.info('预期结果1.显示弹窗，有编辑文件夹、删除文件夹、新建文件夹三个功能键选择，点击空白区域可取消选择')
        device.refresh_layout()
        menu = device.get_element_by_type('Menu')
        menu_area = device.get_element_area(menu)
        menu_snapshot = device.save_snapshot_to_local('menu.jpeg')
        Images.crop_picture(menu_snapshot, *menu_area)
        similarity = Images.compare_image_similarity(menu_snapshot, self.app.edit_folder_menu_pic)
        assert similarity > 0.6, '长按文件夹弹窗显示异常'

        edit_folder = device.get_element_by_text('编辑文件夹')
        delete_folder = device.get_element_by_text('删除文件夹')
        newly_folder = device.get_element_by_text('新建文件夹')
        assert edit_folder, '[编辑文件夹]功能未出现'
        assert delete_folder, '[删除文件夹]功能未出现'
        assert newly_folder, '[新建文件夹]功能未出现'
        device.click(600, 1030)
        device.refresh_layout()
        device.assert_text_exist('编辑文件夹', expect=False), '点击空白区域没有取消选择'
        device.assert_text_exist('删除文件夹', expect=False), '点击空白区域没有取消选择'
        device.assert_text_exist('新建文件夹', expect=False), '点击空白区域没有取消选择'

        logging.info('步骤2.选择编辑文件夹按钮')
        device.long_click_element(personal)
        device.click_element(edit_folder)
        logging.info('预期结果2.正常弹出文件夹编辑框')
        device.refresh_layout()
        device.assert_text_exist('编辑文件夹')
        device.assert_text_exist('取消')
        device.assert_text_exist('保存')
        device.click_element(device.get_element_by_text('取消'))

        logging.info('步骤3.选择删除文件夹按钮')
        device.long_click_element(personal)
        device.click_element(delete_folder)
        logging.info('预期结果3.正常弹出文件夹删除框')
        device.refresh_layout()
        device.assert_text_exist('删除')
        device.assert_text_exist('取消')
        device.click_element(device.get_element_by_text('取消'))

        logging.info('步骤4.选择新建文件夹按钮')
        device.long_click_element(personal)
        device.click_element(newly_folder)
        logging.info('预期结果4.正常弹出新建文件夹弹框')
        device.refresh_layout()
        device.assert_type_exist('TextInput')
        device.assert_text_exist('取消')
        device.assert_text_exist('保存')
        device.click_element(device.get_element_by_text('取消'))
